<?php
namespace Home\Model;
use  Think\Model;

class MoneyModel extends Model{

    public function  create_updatemoney($userId,$geterId,$money,$level,$this_int){

        if($this_int){

            $table = M('repeartmoney');
        }else{

            $table = M('updatemoney');
        }

        $table->startTrans();  // 开启事务

        $create_money1['userId'] = $userId;
        $create_money1['geterId'] = $geterId;
        $create_money1['money'] = $money;
        $create_money1['level'] = $level;
        $create_money1['status'] = 1;
        $create_money1['time'] = time();

        $res1 = $table->add($create_money1);

        if(!$res1){

            $table->rollback();
        }

        $create_money2['userId'] = $geterId;
        $create_money2['geterId'] = $userId;
        $create_money2['money'] = -$money;
        $create_money2['level'] = $level;
        $create_money2['status'] = 2;
        $create_money2['time'] = time();

        $res2 = $table->add($create_money2);

        if(!$res2){

            $table->rollback();
        }

        if($res1 && $res2){

            $table->commit();

            return $res1;
        }

    }
}